<template>
  <div>
    <!-- 头部 -->
    <header :style="bgcolor">
      <van-icon
        name="wap-nav"
        size="24"
        :color="color"
        style="margin-top:20px"
      />
      <p>
        <b>新蜂商城</b>
        <span>山河无恙，人间皆安</span>
      </p>
      <span :style="wcolor" @click="$router.replace('/login')">登录</span>
    </header>
    <!-- banner图 -->
    <div class="banr">
      <van-swipe :autoplay="3000">
        <van-swipe-item v-for="(item, index) in banr" :key="index">
          <img v-lazy="item.carouselUrl" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <!-- 图片nav -->
    <div class="nav">
      <ul>
        <li v-for="(item, index) in nav" :key="index">
          <img :src="item.a" alt="" />
          <span>{{ item.b }}</span>
        </li>
      </ul>
    </div>
    <!-- 商品列表 -->
    <div class="list">
      <div class="list-txt" v-for="(val, index) in list" :key="index">
        <h5>{{ val.a }}</h5>
        <ul>
          <li
            v-for="(item, index) in val.b"
            :key="index"
            @click="$router.push(`/detail`)"
          >
            <img :src="item.goodsCoverImg" alt="" />
            <p>{{ item.goodsName }}</p>
            <b>￥{{ item.sellingPrice }}</b>
          </li>
        </ul>
      </div>
    </div>
    <!-- btoBar -->
    <div class="btoBar">
      <van-tabbar
        v-model="active"
        active-color="#1baeae"
        inactive-color="#222333"
      >
        <van-tabbar-item icon="wap-home">首页</van-tabbar-item>
        <van-tabbar-item icon="ascending">分类</van-tabbar-item>
        <van-tabbar-item icon="shopping-cart">购物车</van-tabbar-item>
        <van-tabbar-item icon="friends">我的</van-tabbar-item>
      </van-tabbar>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      active: 0,
      banr: [],
      bgcolor: "",
      color: "#1baeae",
      wcolor: "color:#1baeae;",
      nav: [
        { a: require("@/assets/nav1.png"), b: "新蜂超市" },
        { a: require("@/assets/nav2.png"), b: "新蜂服饰" },
        { a: require("@/assets/nav3.png"), b: "全球购" },
        { a: require("@/assets/nav4.png"), b: "新蜂生鲜" },
        { a: require("@/assets/nav5.png"), b: "新蜂到家" },
        { a: require("@/assets/nav6.png"), b: "充值缴费" },
        { a: require("@/assets/nav7.png"), b: "9.9元拼" },
        { a: require("@/assets/nav8.png"), b: "领劵" },
        { a: require("@/assets/nav9.png"), b: "省钱" },
        { a: require("@/assets/nav10.png"), b: "全部" },
      ],
      list: [
        { a: "新品上线", b: [] },
        { a: "热门商品", b: [] },
        { a: "最新推荐", b: [] },
      ],
    };
  },
  mounted() {
    //监听页面滚动
    window.addEventListener("scroll", this.windowScroll);

    axios.get("/home").then(({ data }) => {
      const list = data.data;
      this.banr = list.carousels;
      this.list[0].b = list.newGoodses;
      this.list[1].b = list.hotGoodses;
      this.list[2].b = list.recommendGoodses;
    });
  },
  methods: {
    windowScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      if (scrollTop) {
        this.bgcolor = "background:#1baeae;";
        (this.color = "#fff"), (this.wcolor = "color:#fff;");
      } else if (scrollTop == 0) {
        this.bgcolor = "";
        (this.color = "#1baeae"), (this.wcolor = "color:#1baeae;");
      }
    },
    destroyed() {
      window.removeEventListener("scroll", this.windowScroll); //销毁滚动事件
    },
  },
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
li {
  list-style: none;
}
a {
  text-decoration: none;
}
.banr {
  width: 100%;
  height: 216px;
}
.van-swipe,
.van-swipe img {
  width: 100%;
  height: 100%;
}
header {
  width: 100%;
  height: 64px;
  line-height: 64px;
  position: fixed;
  z-index: 100;
  top: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 4%;
  box-sizing: border-box;
  /* background: #1baeae; */
}
header > p {
  width: 70%;
  height: 40px;
  line-height: 40px;
  background: hsla(0, 0%, 100%, 0.7);
  border-radius: 32px;
  margin-top: 12px;
  display: flex;
  overflow: hidden;
}
header > p > b {
  display: block;
  height: 30px;
  line-height: 30px;
  padding: 0 4%;
  margin-top: 6px;
  color: #1baeae;
  font-size: 25px;
  font-weight: 700;
  border-right: 0.02667rem solid #666;
}
header > p > span {
  display: block;
  height: 40px;
  line-height: 40px;
  font-size: 16px;
  color: #666;
  padding-left: 22px;
}
header > span {
  color: #1baeae;
  font-size: 20px;
}
.nav {
  height: 206px;
  background: #fff;
  padding-top: 16px;
}
.nav > ul {
  width: 100%;
  height: 100%;
  display: flex;
  flex-wrap: wrap;
}
.nav > ul > li {
  width: 20%;
  height: 103px;
}
.nav > ul > li > img {
  width: 51px;
  height: 51px;
  display: block;
  margin: 16px auto 10px;
}
.nav > ul > li > span {
  display: block;
  color: #2c3e50;
  font-size: 12px;
  text-align: center;
  width: 100%;
  line-height: 16px;
}
.list {
  background: #fff;
  padding-bottom: 100px;
}
.list-txt {
  background: #e9e9e9;
}
.list > .list-txt > h5 {
  background: #f9f9f9;
  height: 64px;
  line-height: 64px;
  text-align: center;
  color: #1baeae;
  font-size: 21px;
  font-weight: 500;
}
.list-txt ul {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.list-txt ul li {
  background: #fff;
  width: 49.8%;
  height: 276px;
  border-bottom: 1px solid #e9e9e9;
  box-sizing: border-box;
  padding: 13px;
}
.list-txt ul li img {
  display: block;
  width: 154px;
  height: 154px;
  margin: 13px auto;
}
.list-txt ul li p {
  width: 100%;
  height: 21px;
  line-height: 21px;
  text-align: center;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  text-overflow: ellipsis;
  -webkit-box-orient: vertical;
  font-size: 18px;
  color: #222333;
}
.list-txt ul li b {
  margin-top: 8px;
  display: block;
  width: 100%;
  color: #1baeae;
  font-size: 18px;
  height: 21px;
  line-height: 21px;
  text-align: center;
}
</style>
